package com.hpq.common.event.listener;

import com.hpq.common.domain.enums.IdempotentEnum;
import com.hpq.common.event.UserRegisterEvent;
import com.hpq.user.dao.UserDAO;
import com.hpq.user.domain.enums.ItemEnum;
import com.hpq.user.domain.model.User;
import com.hpq.user.service.UserBackpackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 *  用户注册监听器
 */
@Component
@Slf4j
public class UserRegisterListener {
    @Autowired
    private UserDAO userDAO;
    @Autowired
    private UserBackpackService userBackpackService;

    @TransactionalEventListener(value = UserRegisterEvent.class,phase = TransactionPhase.AFTER_COMMIT)
    public void sendCard(UserRegisterEvent event){
        User user = event.getUser();
        userBackpackService.acquireItem(user.getId(), ItemEnum.MODIFY_NAME_CARD.getId(), IdempotentEnum.UID, user.getId().toString());
        log.info("改名卡发放成功！");
    }
    @TransactionalEventListener(value = UserRegisterEvent.class,phase = TransactionPhase.AFTER_COMMIT)
    public void sendBadge(UserRegisterEvent event){
        User user = event.getUser();
        int count = userDAO.count();
        if(count<=10){
        userBackpackService.acquireItem(user.getId(), ItemEnum.REG_TOP10_BADGE.getId(), IdempotentEnum.UID, user.getId().toString());
        }else if(count<=100){
        userBackpackService.acquireItem(user.getId(), ItemEnum.REG_TOP100_BADGE.getId(), IdempotentEnum.UID, user.getId().toString());
        }
        log.info("徽章发放成功！");
    }

}
